﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class AddNewBranch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check user login or not
        if (Session["Username"] == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
            if (Session["RoleName"].ToString() == "Employee")
            {
                Response.Redirect("ValidateUser.aspx");
            }
    }
    protected void imgbAdd_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["OPOConnectionString"].ToString());
        cnn.Open();

        SqlCommand cmd = new SqlCommand("Insert into Branch (BranchName, BranchAddress, BranchPhone, BranchEmail, CityID)"
                                        + "values (@BranchName, @BranchAddress, @BranchPhone, @BranchEmail, @CityID)", cnn);

        cmd.Parameters.Add(new SqlParameter("@BranchName", SqlDbType.NVarChar, 50));
        cmd.Parameters["@BranchName"].Value = txtBName.Text;
        cmd.Parameters.Add(new SqlParameter("@BranchAddress", SqlDbType.NVarChar, 1000));
        cmd.Parameters["@BranchAddress"].Value = txtBAddress.Text;
        cmd.Parameters.Add(new SqlParameter("@BranchPhone", SqlDbType.NVarChar, 20));
        cmd.Parameters["@BranchPhone"].Value = txtBPhone.Text;
        cmd.Parameters.Add(new SqlParameter("@BranchEmail", SqlDbType.NVarChar, 50));
        cmd.Parameters["@BranchEmail"].Value = txtBEmail.Text;
        cmd.Parameters.Add(new SqlParameter("@CityID", SqlDbType.Int));
        cmd.Parameters["@CityID"].Value = ddlCity.SelectedValue;
        cmd.ExecuteNonQuery();

        ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "ShowMessage()", true);
    }
    protected void imgbReset_Click(object sender, ImageClickEventArgs e)
    {
        ddlCity.SelectedIndex = 0;
        txtBName.Text = "";
        txtBAddress.Text = "";
        txtBPhone.Text = "";
        txtBEmail.Text = "";
    }
    protected void imgbCancel_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("AdminHomePage.aspx");
    }
}